﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Search : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }
	protected void btnSearch_Click(object sender, EventArgs e)
	{
		string querystring = "";
		if (!string.IsNullOrEmpty(txtSearch.Text.Trim()))
			AddQueryParam(ref querystring, "q=" + Server.UrlEncode(txtSearch.Text));
		if (dpStart.SelectedDate.Year > 1900)
			AddQueryParam(ref querystring, "start=" + dpStart.SelectedDate.ToString("MM/dd/yyyy"));
		if (dpStop.SelectedDate.Year > 1900)
			AddQueryParam(ref querystring, "stop=" + dpStop.SelectedDate.ToString("MM/dd/yyyy"));

		AddQueryParam(ref querystring, "type=" + rblSearchType.SelectedValue);
		AddQueryParam(ref querystring, "orderby=" + ddlOrderby.SelectedItem.Value);
		switch (rblCaption.SelectedValue) {
			case "true": AddQueryParam(ref querystring, "caption=true"); break;
			case "false": AddQueryParam(ref querystring, "caption=false"); break;
			default: break; }

		int maxResults = 0;
		if (int.TryParse(txtMaxResults.Text, out maxResults))
			AddQueryParam(ref querystring, "max=" + maxResults.ToString());

		Response.Redirect("results.aspx" + querystring, false);
	}
	public void AddQueryParam(ref string querystring, string param)
	{
		if (querystring == null) querystring = "";
		querystring += string.IsNullOrEmpty(querystring) ? "?" : "&";
		querystring += param;
	}
}